package myFilter;

import myFilter.dto.Request;
import myFilter.dto.Response;
import myFilter.filter.Filter;

import java.util.ArrayList;

/**
 * @author yonyong
 **/
public class FilterChain implements Filter {
    private int index = 0;
    private ArrayList<Filter> arrayList = new ArrayList<>(0);

    public Boolean status = false;

    public void addFilter(Filter filter) {
        arrayList.add(filter);
    }

    @Override
    public void doFilter(Request request, Response response, FilterChain filterChain) {
        if (index == arrayList.size()) {
            status = true;
            return;
        }

        new Thread(() -> {
            index++;
            Filter filter = arrayList.get(index - 1);
            Thread.currentThread().setName("Task-" + filter.getClass().getSimpleName());
            System.out.println(Thread.currentThread().getName());

            filter.doFilter(request, response, filterChain);

            System.out.println("result: " + request.getVal());
        }).start();
    }
}
